<?php

require_once(CONFIG_DIR . 'reg_product_config.php');
require_once(LIB_DIR . 'api/general.php');

$shopDAO = GetShopDAO();
$productDAO = GetProductDAO();
$cityDAO = GetCityDAO();
$productCategoryDAO = GetProductCategoryDAO();
$errmsg = GetMessageStack();

//get by sid
$platID = "";
$nickName = "";

$city = "";
$province = "";
if (!isset($_GET['sid']) || !is_numeric($_GET['sid'])) {
    edp_redirect(edp_href_link(PAGENAME_PAGE_NOT_FOUND));
} else {

    $sid = $_GET['sid'];

    if (!$shopDAO->GetShopName($sid)) {

        edp_redirect(edp_href_link(PAGENAME_PAGE_NOT_FOUND));
    }
}
$platID = $shopDAO->GetPlatID($sid);
if (!$platID) {
    edp_redirect(edp_href_link(PAGENAME_PAGE_NOT_FOUND));
}

$status = $shopDAO->GetShopStatus($sid); //stauts为2,5,9不能添加商品
if ($status == ADDSHOP_STATUS_UNKOWNN_PLATFORM || $status == ADDSHOP_STATUS_MISS_KEY_DATA) {
    $errmsg->add( 'reg', '店铺状态不正常！' );
    TransferTo('not_allow_add_product');
    return;
}

// 主逻辑
$shopInfoTaobao = $shopDAO->GetShopInfoTaobao( $sid );
require_once(LIB_DIR . 'api/taobao/Taoapi.php');

// get taobao seller_id
$Taoapi = new Taoapi;
$Taoapi->method = 'taobao.taobaoke.shops.convert';
$Taoapi->fields = 'user_id,shop_title,click_url,commission_rate';
$Taoapi->sids = $shopInfoTaobao['taobao_id'];
$Taoapi->pid = TAOBAOKE_ID;
$TaobaokeData = $Taoapi->Send('get', 'xml')->getArrayData();

if (edp_is_empty_string($Taoapi->getErrorInfo()) && !empty($TaobaokeData)) { 
    // get all taobaoke product
    $Taoapi = new Taoapi;
    $Taoapi->method = 'taobao.taobaoke.items.relate.get';
    $Taoapi->fields = 'num_iid,seller_id, nick, title, price, item_location, pic_url, click_url,commission,commission_rate,commission_num,commission_volume';
    $Taoapi->relate_type = 4;
    $Taoapi->seller_id = $TaobaokeData['taobaoke_shops']['taobaoke_shop']['user_id']; 
    $Taoapi->pid = TAOBAOKE_ID;
    $products = $Taoapi->Send('get', 'xml')->getArrayData();
    
    if (edp_is_empty_string($Taoapi->getErrorInfo()) && !empty($products)) { 
        foreach( $products['taobaoke_items']['taobaoke_item'] as $product ){
            // check added or not
            $extid = $productDAO->GetProductIDByExtID( $product['num_iid'], 'taobao' );
            if( $extid !== false){
                echo $product['title']."already exitst <br />";
                continue;
            }
            $Taoapi = new Taoapi;
            $Taoapi->method = 'taobao.item.get';
            $Taoapi->fields = 'num_iid,title,price,cid,detail_url';
            $Taoapi->num_iid = $product['num_iid'];
            $productTaobaoInfo = $Taoapi->Send('get', 'xml')->getArrayData();
            if (edp_is_empty_string($Taoapi->getErrorInfo()) && !empty($productTaobaoInfo)) { 
                $cateid = $productCategoryDAO->ConvertTaobaoProductCid($productTaobaoInfo['item']['cid'] );
                ProcessAddProduct( $platID, $sid, $product['title'], $productTaobaoInfo['item']['detail_url'], $product['click_url'], 
                        $product['pic_url'], $product['price'], $cateid, $product['item_location'], $product['num_iid'] );
            }
            else{
                echo "获取 ".$product['title']."详细信息失败 <br />";
                continue;
            }
        }
    }
    else{
        $errmsg->add( 'reg', '获取淘宝店铺商品失败！' );
        TransferTo('not_allow_add_product');
        return;
    }
}
else{
    $errmsg->add( 'reg', '获取淘宝店铺信息失败！' );
    TransferTo('not_allow_add_product');
    return;
}

// when  everything is OK
edp_redirect(edp_href_link(PAGENAME_PRODUCT_SUMMARY, "sid=$sid"));

function ProcessAddProduct( $platID, $sid, $productName, $productURL, $clickURL, $productPicture, $productPrice, $productCategory, $productLocationStr, $extProductID ) {
    global $shopDAO, $productDAO, $cityDAO, $status;
    
//    $status = $shopDAO->GetShopStatus($sid); //stauts为2,5,9不能添加商品
//    if($status == ADDSHOP_STATUS_UNKOWNN_PLATFORM || $status == ADDSHOP_STATUS_MISS_KEY_DATA )
//    {
//        $errmsg->add( 'reg', '获取淘宝店铺商品失败！' );
//        TransferTo('not_allow_add_product');
//        return;				
//    }
        
    // productDesc = '';   
    $userID = edp_getsession('userid');
    $arrProductLocation = explode( " ", $productLocationStr );
    if( count($arrProductLocation) == 2 ){
        $productLocation = $cityDAO->GetCityID( $arrProductLocation[1] );
    }
    else{
        $productLocation = 3;
    }
    
    switch ($platID) {

        case '0': {
                $errmsg->add( 'reg', 'platid 非法' );
                TransferTo('not_allow_add_product');
                break;
            }

        case '1': {
                //不需要拼接URL，存入数据库的已经是正常的URL
                $productID = $productDAO->InsertTaobaoProductData($productCategory, $sid, $productName, $productURL, $productPicture, $productLocation, $productPrice, $productDesc, $status, $userID, $extProductID, $clickURL, '0');

                if( $productID === false ){
                    echo "$productName 添加失败！<br />";
                }
                else{
                    echo "$productName 添加成功！<br />";
                }
                break;
            }
        

        default: {
                TransferTo('not_allow_add_product');
                break;
            }
    }

//    edp_redirect(edp_href_link(PAGENAME_PRODUCT_SUMMARY, "sid=$sid"));
}

?>
